package com.books.controller.index;/*
 *@Auther Zhourui
 *@data 2021/9/25  22:11
 *@Email 1605576647@qq.com
 */

import com.books.dao.NewQuestionDao;
import com.books.model.entity.backcontroller.Announcement;
import com.books.model.entity.book.Books;
import com.books.model.entity.hotrecommend.Digest;
import com.books.model.entity.login_regis.UserLogin;
import com.books.model.entity.question.NewQuestion;
import com.books.service.admin.impl.AdminServiceImpl;
import com.books.service.book.IBookService;
import com.books.service.hotrecommend.impl.DigestServiceImpl;
import com.books.service.people.IPeopleService;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexOneController {

    @Autowired(required = false)
    private IBookService bookService;

    @Autowired(required = false)
    private DigestServiceImpl digestService;

//    @Autowired(required = false)
//    private RedisTemplate redisTemplate;


    @Autowired(required = false)
    private AdminServiceImpl adminService;

    @Autowired(required = false)
    private IPeopleService peopleService;

    @Autowired(required = false)
    private NewQuestionDao newQuestionDao;

    @GetMapping("/index01")
    public String index01Book(HttpServletRequest request, String name, Integer bookTypeId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "14") Integer pageSize)
    {
        PageInfo<Books> pageInfo1;
        PageInfo<Books> pageInfo2;
        PageInfo<Books> pageInfo3;
        PageInfo<Books> pageInfo4;
        PageInfo<Books> pageInfo5;
        PageInfo<Books> pageInfo6;
        PageInfo<Books> pageInfo7;
        PageInfo<Books> pageInfo8;
        pageInfo1 = bookService.index(name,1,pageNum,pageSize);
        pageInfo2 = bookService.index(name,2,pageNum,pageSize);
        pageInfo3 = bookService.index(name,10,pageNum,pageSize);
        pageInfo4 = bookService.index(name,4,pageNum,pageSize);
        pageInfo5 = bookService.index(name,5,pageNum,pageSize);
        pageInfo6 = bookService.index(name,6,pageNum,pageSize);
        pageInfo7 = bookService.index(name,7,pageNum,pageSize);
        pageInfo8 = bookService.index(name,8,pageNum,pageSize);
//        if(redisTemplate.opsForValue().get("pageInfo1")==null){
//            pageInfo1 = bookService.index(name,1,pageNum,pageSize);
//            redisTemplate.opsForValue().set("pageInfo1",pageInfo1,1, TimeUnit.DAYS);
//        }
//        else {
//            pageInfo1= (PageInfo<Books>) redisTemplate.opsForValue().get("pageInfo1");
//        }
//        if(redisTemplate.opsForValue().get("pageInfo2")==null){
//            pageInfo2 = bookService.index(name,2,pageNum,pageSize);
//            redisTemplate.opsForValue().set("pageInfo2",pageInfo2,1, TimeUnit.DAYS);
//        }
//        else {
//            pageInfo2= (PageInfo<Books>) redisTemplate.opsForValue().get("pageInfo2");
//        }
//        if(redisTemplate.opsForValue().get("pageInfo3")==null){
//            pageInfo3 = bookService.index(name,10,pageNum,pageSize);
//            redisTemplate.opsForValue().set("pageInfo3",pageInfo3,1, TimeUnit.DAYS);
//        }
//        else {
//            pageInfo3= (PageInfo<Books>) redisTemplate.opsForValue().get("pageInfo3");
//        }
//        if(redisTemplate.opsForValue().get("pageInfo4")==null){
//            pageInfo4 = bookService.index(name,4,pageNum,pageSize);
//            redisTemplate.opsForValue().set("pageInfo4",pageInfo4,1, TimeUnit.DAYS);
//        }
//        else {
//            pageInfo4= (PageInfo<Books>) redisTemplate.opsForValue().get("pageInfo4");
//        }
//        if(redisTemplate.opsForValue().get("pageInfo5")==null){
//            pageInfo5 = bookService.index(name,5,pageNum,pageSize);
//            redisTemplate.opsForValue().set("pageInfo5",pageInfo5,1, TimeUnit.DAYS);
//        }
//        else {
//            pageInfo5= (PageInfo<Books>) redisTemplate.opsForValue().get("pageInfo6");
//        }
//        if(redisTemplate.opsForValue().get("pageInfo6")==null){
//            pageInfo6 = bookService.index(name,6,pageNum,pageSize);
//            redisTemplate.opsForValue().set("pageInfo6",pageInfo6,1, TimeUnit.DAYS);
//        }
//        else {
//            pageInfo6= (PageInfo<Books>) redisTemplate.opsForValue().get("pageInfo6");
//        }
//        if(redisTemplate.opsForValue().get("pageInfo7")==null){
//            pageInfo7 = bookService.index(name,7,pageNum,pageSize);
//            redisTemplate.opsForValue().set("pageInfo7",pageInfo7,1, TimeUnit.DAYS);
//        }
//        else {
//            pageInfo7= (PageInfo<Books>) redisTemplate.opsForValue().get("pageInfo7");
//        }
//        if(redisTemplate.opsForValue().get("pageInfo8")==null){
//            pageInfo8 = bookService.index(name,8,pageNum,pageSize);
//            redisTemplate.opsForValue().set("pageInfo8",pageInfo8,1, TimeUnit.DAYS);
//        }
//        else {
//            pageInfo8= (PageInfo<Books>) redisTemplate.opsForValue().get("pageInfo8");
//        }
        request.setAttribute("pageInfo1",pageInfo1);
        request.setAttribute("pageInfo2",pageInfo2);
        request.setAttribute("pageInfo3",pageInfo3);
        request.setAttribute("pageInfo4",pageInfo4);
        request.setAttribute("pageInfo5",pageInfo5);
        request.setAttribute("pageInfo6",pageInfo6);
        request.setAttribute("pageInfo7",pageInfo7);
        request.setAttribute("pageInfo8",pageInfo8);

        List<Digest> digests1 = digestService.findByClassify("文学");
        List<Digest> digests2 = digestService.findByClassify("青春");
        List<Digest> digests3 = digestService.findByClassify("经典");
        request.setAttribute("digests1",digests1);
        request.setAttribute("digests2",digests2);
        request.setAttribute("digests3",digests3);

        List<Announcement> announcements = adminService.findAllAnnouncement();
        request.setAttribute("announcements",announcements);

        HttpSession session = request.getSession();
        UserLogin user = (UserLogin) session.getAttribute("user");
        if (user!=null){
            Integer peopleId1 = peopleService.findPeopleId(user.getAccount());
            List<NewQuestion> newQuestions = newQuestionDao.findNewQuestionByPeopleId(peopleId1);
            JSONArray newQuestionJson = JSONArray.fromObject(newQuestions);
            request.setAttribute("newQuestions",newQuestionJson);
        }

        return "index01";
    }

    @GetMapping("/publicMsg")
    @ResponseBody
    public Announcement publicMsg(Integer msg){
        List<Announcement> announcements = adminService.findAllAnnouncement();
        Announcement announcement = announcements.get(msg);
        return announcement;
    }



}
